package com.example.demo;

import lombok.Data;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.soap.SOAPBinding;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController//用于http响应
@Data//创建set和get方法
@Slf4j//构造日志对象
public class UserTest {

    private String message;

    @RequestMapping("/log")
    public void func() {
        log.debug("debug log");
        log.trace("trace log");
        log.info("info log");
        log.warn("warn.log");
        log.error("error log");
        System.out.println("");
    }

    @RequestMapping("/test")
    private String func2() {
        return "hello SpringMVC";
    }

    @RequestMapping("/user")
    public UserDemo getUser(UserDemo userDemo) {
//        UserDemo userDemo= new UserDemo();
//        userDemo.setId(id);
//        userDemo.setName(name);;
        return userDemo;
    }

    @RequestMapping("/upload")
    public String upload(@RequestPart("somefile")MultipartFile file) throws IOException {
        //这样的路径每次获取到的图片都会覆盖到上一次的图片
        //String path = "E:\\imageTest\\img.jpg";

        //使用UUID可以获取到一个全球唯一id
        String name = UUID.randomUUID().toString().replace("-","");
        //获取一个文件后缀
        name += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //这样每次存入就可以得到一个不一样的文件名
        String path = "E:\\imageTest\\" + name;
        file.transferTo(new File(path));
        return path;
    }
}
